package com.entitymapper;

import java.lang.reflect.Constructor;

import com.entitymapper.driver.Driver;
import com.entitymapper.driver.DriverManager;
import com.entitymapper.field.ResultSet;

/**
 * 该类定义了管理ResultSet:(读写时的容器(数据集))时，由DriverManager提供可读写的Driver:(驱动)返回一个ResultSet操作<br>
 * 无法完成方法调用时抛出有明确异常信息的 @see com.entitymapper.EntityMapperException类型或子类型 <br>
 * @author hhf
 */
public class ResultSetManager {

	/**
	 * 不同数据对象的读取，所要的读写驱动<br>
	 */
	private Driver driver;
	
	private Constructor cacheResultSetConstructor;
	
	/**
	 * 通过对象的类型来创建一个带有自身Driver(驱动)的一个相对应的ReseltSet
	 * @param entityType 表示对象的类型，如：User.class
	 * @return 带有自身Driver(驱动)的一个相对应的ReseltSet
	 * @throws EntityMapperException
	 */
	protected ResultSet createResultSet(Class<?> entityType) throws EntityMapperException{
		this.driver = DriverManager.getDriver();
		ResultSet rs = null;
		try {
			this.cacheResultSetConstructor = driver.getResultSetClass().getDeclaredConstructor(Class.class);
			rs = (ResultSet) this.cacheResultSetConstructor.newInstance(entityType);
			rs.setDriver(driver);
			return rs;
		} catch (Exception e) {
			throw new EntityMapperException("Fail to create ResultSet:" + this.driver.getResultSetClass(), e);
		} 
	}
	
	public Driver getDriver() {
		return driver;
	}
	public void setDriver(Driver driver) {
		this.driver = driver;
	}
}
